package io.renren.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.entity.ExperimentEntity;
import io.renren.entity.Result;
import io.renren.service.ExperimentService;
import io.renren.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * describe:
 * create @ kylin  2019/03/17
 */
@Api(tags = "实验相关")
@RestController
@RequestMapping(value = "/api/exp")
public class ApiExperimentController {

    @Autowired
    private ExperimentService experimentService;

    @PostMapping(value = "/save")
    @ApiOperation(value = "提交实验预约")
    public Result<ExperimentEntity> save(String exStudent, String exStarttime, @RequestParam("exEndtime") String exEndtime,
                                         @RequestParam("exDepartment")String exDepartment,
                                         String suRoom, String exName, String exContent, String openid) throws Exception{
        ExperimentEntity experimentEntity = new ExperimentEntity();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        experimentEntity.setExStudent(exStudent);
        experimentEntity.setExStarttime(sdf.parse(exStarttime));
        experimentEntity.setExEndtime(sdf.parse(exEndtime));
        experimentEntity.setExDepartment(exDepartment);
        experimentEntity.setExRoom(suRoom);
        experimentEntity.setExName(exName);
        experimentEntity.setExContent(exContent);
        experimentEntity.setOpenid(openid);
        experimentEntity.setUuid(UUID.randomUUID().toString());
        experimentEntity.setExTime(new Date());

        boolean save = this.experimentService.save(experimentEntity);
        if (save){
            return ResultUtil.success("写入成功");
        }else{
            return ResultUtil.success("fail");
        }
    }

    @ApiOperation(value = "获取某个学生的所有预约")
    @GetMapping(value = "my/{openid}")
    public Result<ExperimentEntity> mysubs(@PathVariable("openid") String openid) throws Exception{
        QueryWrapper<ExperimentEntity> queryWrapper = new QueryWrapper();
        return ResultUtil.success(this.experimentService.list(queryWrapper.lambda().eq(ExperimentEntity::getOpenid,openid)));
    }

}
